package com.itheima.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.common.R;
import com.itheima.dto.DishFlavordto;
import com.itheima.dto.SetmealDto;
import com.itheima.entity.Category;
import com.itheima.entity.Dish;
import com.itheima.entity.DishFlavor;
import com.itheima.service.CategoryService;
import com.itheima.service.DishFlavorService;
import com.itheima.service.DishService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@RestController
@RequestMapping("/dish")
public class DishController {
    @Autowired
    private DishService dishService;
    @Autowired
    private CategoryService categoryService;
    @Autowired
    private DishFlavorService dishFlavorService;

    @GetMapping("/page")
    public R<Page> page(int page, int pageSize, String name) {
        //获取分页构造器
        Page<Dish> pages = new Page<>(page, pageSize);
        Page<DishFlavordto> DishFlavordtoPage = new Page<>();
        //获取lambda条件构造器
        LambdaQueryWrapper<Dish> Wrapper = new LambdaQueryWrapper<>();
        //添加过滤条件
        Wrapper.like(name != null, Dish::getName, name);
        //添加排序条件
        Wrapper.orderByDesc(Dish::getUpdateTime);
        //执行条件分页语句
        dishService.page(pages, Wrapper);

        //对象拷贝
        BeanUtils.copyProperties(pages, DishFlavordtoPage, "records");

        List<Dish> records = pages.getRecords();

        List<DishFlavordto> list = records.stream().map((item) -> {
            DishFlavordto DishFlavordto = new DishFlavordto();
            BeanUtils.copyProperties(item, DishFlavordto);
            Long categoryId = item.getCategoryId();//分类id
            //根据id查询分类对象
            Category category = categoryService.getById(categoryId);
            if (category != null) {
                String categoryName = category.getName();
                DishFlavordto.setCategoryName(categoryName);
            }
            return DishFlavordto;
        }).collect(Collectors.toList());

        DishFlavordtoPage.setRecords(list);
        return R.success(DishFlavordtoPage);
    }

    @PostMapping
    public R<String> add(@RequestBody DishFlavordto dishFlavordto) {
        dishService.saveWithFlavor(dishFlavordto);
        return R.success("上传成功");
    }

    @DeleteMapping
    public R<String> deleteById(Long ids) {
        dishService.deleteById(ids);
        return R.success("删除成功");
    }

    @GetMapping("/{ids}")
    public R<DishFlavordto> updateById1(@PathVariable Long ids) {
        DishFlavordto dishFlavordto = dishService.GetByIdWithFlavor(ids);
        return R.success(dishFlavordto);
    }

    /*@GetMapping("/list")
    public R<List<Dish>> listR(Long categoryId){
        log.info(String.valueOf(categoryId));
        LambdaQueryWrapper<Dish> wrapper = new LambdaQueryWrapper<>();
        //添加条件查询
        wrapper.eq(Dish::getCategoryId,categoryId);
        //添加排序条件
        wrapper.orderByDesc(Dish::getUpdateTime).orderByAsc(Dish::getSort);
        List<Dish> list = dishService.list(wrapper);
        return R.success(list);
    }*/
    @GetMapping("/list")
    public R<List<DishFlavordto>> listR(Dish dish){
        LambdaQueryWrapper<Dish> wrapper = new LambdaQueryWrapper<>();
        //添加条件查询
        wrapper.eq(Dish::getCategoryId,dish.getCategoryId());
        //添加排序条件
        wrapper.orderByDesc(Dish::getUpdateTime).orderByAsc(Dish::getSort);
        List<Dish> list = dishService.list(wrapper);
        //拷贝口味表
        List<DishFlavordto> collect = list.stream().map((item) -> {
            DishFlavordto dishFlavordto = new DishFlavordto();
            BeanUtils.copyProperties(item, dishFlavordto);
            Long categoryId = item.getCategoryId();
            Category byId = categoryService.getById(categoryId);
            if (byId!=null){
                String name = byId.getName();
                dishFlavordto.setCategoryName(name);
            }
            Long id = item.getId();//菜品id
            LambdaQueryWrapper<DishFlavor> wrapper1 = new LambdaQueryWrapper<>();
            wrapper1.eq(DishFlavor::getDishId,id);
            List<DishFlavor> list1 = dishFlavorService.list(wrapper1);
            dishFlavordto.setFlavors(list1);
            return dishFlavordto;
        }).collect(Collectors.toList());
        return R.success(collect);
    }

    @GetMapping
    public R<String> get(SetmealDto setmealDto){
        dishService.saveWithDish(setmealDto);
        return R.success("添加成功了");
    }


    @PutMapping
    public R<String> updateById2(@RequestBody DishFlavordto dishFlavordto){
        dishService.UpdateWithById(dishFlavordto);
        return R.success("success");
    }
}
